package com.wiwf.model.manage.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.springframework.util.ObjectUtils;

public class SeleniumUtil {
	
	private static FirefoxDriver FOX_WEB_DRIVE;

	/**
	 * 获取 selenium firefox Driver
	 * @return FirefoxDriver
	 */
	public static FirefoxDriver getFoxDriver(){
		return getFoxDriver("/data/spider/file/geckodriver.exe");
	}
	
	/**
	 * 获取 selenium firefox Driver
	 * @param driverUrl /data/spider/file/geckodriver.exe
	 * @return FirefoxDriver
	 */
	public static FirefoxDriver getFoxDriver(String driverUrl){
		if(! ObjectUtils.isEmpty(SeleniumUtil.FOX_WEB_DRIVE)) {
			return SeleniumUtil.FOX_WEB_DRIVE;
		}
		
		System.setProperty("webdriver.gecko.driver", driverUrl);
		
		//------------------
    	FirefoxOptions options = new FirefoxOptions();
    	FirefoxProfile profile = new FirefoxProfile();
    	
    	//------------------
    			// 表示自定义下载目录
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", "/data/firefox/download");
        		// 不提示保存对话框
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/pdf");

        		// 设置持久保存会话和 Cookie
        profile.setPreference("browser.cache.disk.enable", true);
        profile.setPreference("browser.cache.memory.enable", true);
        profile.setPreference("browser.cache.offline.enable", true);
        profile.setPreference("network.cookie.cookieBehavior", 0); // 0 表示接受所有 Cookie
        profile.setPreference("network.cookie.lifetime.days", -1); // -1 表示永久保存 Cookie
        profile.setPreference("signon.autofillForms", true); // 自动填充表单
        profile.setPreference("signon.expireMasterPassword", false); // 不过期主密码
        //------------------
    	options.setProfile(profile);
    	
    	
    	SeleniumUtil.FOX_WEB_DRIVE = new FirefoxDriver(options);
    	
    	return SeleniumUtil.FOX_WEB_DRIVE;
	}
	
	/**
	 * 节点高亮 
	 * @param xpath
	 */
	public static int tagHig(String xpath) {
		int idx = 1;
		for (WebElement webElement : FOX_WEB_DRIVE.findElements(By.xpath(xpath))) {
			String yStyle = webElement.getAttribute("style");
			String nextStyle = yStyle + "3px solid red";
			FOX_WEB_DRIVE.executeScript(
					"arguments[0].style.border='"+nextStyle+"';", webElement);
			FOX_WEB_DRIVE.executeScript(
			        "var node_"+idx+"=arguments[0];"+
					"setTimeout(function() { node_"+idx+".style.border='"+yStyle+"'; }, 1000*4);", webElement);
			idx ++;
		}
		return idx-1;
	}
	/**
	 * 节点数量
	 * @param xpath
	 * @return 
	 */
	public static int tagSize(String xpath) {
		return FOX_WEB_DRIVE.findElements(By.xpath(xpath)).size();
	}
	/**
	 * 节点集合
	 * @param xpath
	 * @return 
	 */
	public static List<WebElement> tagList(String xpath) {
		return FOX_WEB_DRIVE.findElements(By.xpath(xpath));
	}
	
	public static void screenshot(String xpath, String URL) throws IOException{
		WebElement findElement = FOX_WEB_DRIVE.findElement(By.xpath(xpath));
		File screenshotAs = findElement.getScreenshotAs(OutputType.FILE);
		
		Files.copy(Paths.get(screenshotAs.getPath()), Paths.get(URL+"/"+screenshotAs.getName()), StandardCopyOption.REPLACE_EXISTING);
	}
	
	/**
	 * 跳转地址
	 * @param URL
	 */
	public static void getUrl(String URL) {
		String currentUrl = FOX_WEB_DRIVE.getCurrentUrl();
		if(! currentUrl.equals(URL)) {
			FOX_WEB_DRIVE.get(URL);
		}
	}
	
	/**
	 * 结束 drive
	 * @param xpath
	 */
	public static void quit() {
		if(! ObjectUtils.isEmpty(FOX_WEB_DRIVE)) {
			SeleniumUtil.FOX_WEB_DRIVE.quit();
			SeleniumUtil.FOX_WEB_DRIVE = null;
		}
	}
	
}
